<?php
class TestsetupController extends Zend_Controller_Action 
{
    function init()
    {
        $this->initView();
        Zend_Loader::loadClass('AuxDbTable');
        Zend_Loader::loadClass('User');
        Zend_Loader::loadClass('Test');
        Zend_Loader::loadClass('Authgroup');
        Zend_Loader::loadClass('Mainmenu');
        Zend_Loader::loadClass('Rights');
        Zend_Loader::loadClass('Authority'); 
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->user = Zend_Auth::getInstance()->getIdentity();
        $this->view->complexitys = array(1=>'Low',2=>'Middle',3=>'High');
    }
    
    function preDispatch()
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('auth/login');
        }
        $menu = new Mainmenu();
        $this->view->main_menu = $menu->getMenu($this->view->user->id);
    }

    function indexAction()
    {
        $Rights = new Rights();
        //if(!$Rights->CanDo($this->view->user->id,'test','index')) $this->_redirect(DEFAUL_URL);
        
        $this->view->title = "Tests";
        $this->view->content_title = "Tests";
        $test = new Test();
        
        $this->view->item = new stdClass();
        
        $this->view->item->complexity = $this->_request->getParam('complexity', 0)?$this->_request->getParam('complexity', 0):0;
        
        $cond = array('1=1');
        foreach($this->view->item as $key=>$value) {
            if($value) {
                switch($key) {
                    default :
                        $cond[] = $key.'="'.$value.'"';
                        break;
                }
            }
        }
        
        $this->view->items = $test->fetchAll($cond,'complexity');
        foreach($this->view->items as $item) {
            $item->complexity = $this->view->complexitys[$item->complexity];
        }
        
        $this->view->can_add = true;//$Rights->CanDo($this->view->user->id,'test','add');
        $this->view->can_edit = true;//$Rights->CanDo($this->view->user->id,'test','edit');
        $this->view->can_delete = true;//$Rights->CanDo($this->view->user->id,'test','delete');
        
        $this->render();
    }
    
    function validate($id,$params)
    {
        $valid = true;
        $obj = new Test();
        
        if(strlen($params['question'])==0)
        {
            $this->view->message .= 'Please, enter Question.<br>';
            $valid = false;
        }    
        if(strlen($params['answers'])==0)
        {
            $this->view->message .= 'Please, enter Answers.<br>';
            $valid = false;
        }    
        if(strlen($params['correct'])==0)
        {
            $this->view->message .= 'Please, enter Correct.<br>';
            $valid = false;
        }    
                
        
        //email
        $email_pattern = '/^[A-Z0-9\._\-]+@[A-Z0-9\.\-]+\.[A-Z]{2,6}$/i';
        
        //date 
        $date_pattern = '/^[0-9]{4}\-(?:0[1-9]|1[0-2])\-(?:[0-2]?[0-9]|3[0-1])$/';
        //time
        $time_pattern = '^(([0-9])|([0-1][0-9])|([2][0-3])):(([0-9])|([0-5][0-9]))$';
                
        return $valid;
    }
    function addAction()
    {
        $Rights = new Rights();
        //if(!$Rights->CanDo($this->view->user->id,'test','add')) $this->_redirect(DEFAUL_URL);
        
        $this->view->content_title = $this->view->title = "Add New Test";
        $params = array();

        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $filter = new Zend_Filter_StripTags();
            
            $params['question'] = trim($filter->filter($this->_request->getPost('question')));
            $params['answers'] = trim($filter->filter($this->_request->getPost('answers')));
            $params['correct'] = trim($filter->filter($this->_request->getPost('correct')));
            $params['complexity'] = trim($filter->filter($this->_request->getPost('complexity')));
                    
            $this->view->message = '';
            if($this->validate(0,$params)) {
                $obj = new Test();
                $new_id = $obj->insert($params);
                
                $this->_redirect('/testsetup');
                return;
            }
        } 
        
        // set up an "empty" test        
        $this->view->item = new stdClass();
        $this->view->item->id = null;
        $this->view->item->question = isset($params['question'])?$params['question']:'';
        $this->view->item->answers = isset($params['answers'])?$params['answers']:'';
        $this->view->item->correct = isset($params['correct'])?$params['correct']:'';
        $this->view->item->complexity = isset($params['complexity'])?$params['complexity']:'';
                        
                    
        // additional view fields required by form
        $this->view->action = 'add';
        $this->render();
    }
    
    function editAction()
    {
        $Rights = new Rights();
        //if(!$Rights->CanDo($this->view->user->id,'test','edit')) $this->_redirect(DEFAUL_URL);
            
        $this->view->content_title = $this->view->title = "Edit Test";
        $obj = new Test();
        
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $filter = new Zend_Filter_StripTags();
            
            $id = (int)$this->_request->getPost('id');
            $params = array();
            $params['question'] = trim($filter->filter($this->_request->getPost('question')));
            $params['answers'] = trim($filter->filter($this->_request->getPost('answers')));
            $params['correct'] = trim($filter->filter($this->_request->getPost('correct')));
            $params['complexity'] = trim($filter->filter($this->_request->getPost('complexity')));
                        
            if ($id !== false) {
                $this->view->message = '';
                if($this->validate($id,$params)) {
                    $where = 'id = ' . $id;
                    $obj->update($params, $where);
                
                    $this->_redirect('/testsetup');
                    return;
                } else {
                    $this->view->item = $obj->fetchRow('id='.$id);
                    $this->view->item->id = $id;
                    
                    if( $params['question']) $this->view->item->question = $params['question'];
                    if( $params['answers']) $this->view->item->answers = $params['answers'];
                    if( $params['correct']) $this->view->item->correct = $params['correct'];
                    if( $params['complexity']) $this->view->item->complexity = $params['complexity'];
                                    
                }
            }
        } else {
            $id = (int)$this->_request->getParam('id', 0);
            if ($id > 0) {
                $this->view->item = $obj->fetchRow('id='.$id);
            }
        }

        // additional view fields required by form
        $this->view->action = 'edit';
        
        $this->render();
    }
    
    function deleteAction()
    {
        $Rights = new Rights();
        //if(!$Rights->CanDo($this->view->user->id,'test','delete')) $this->_redirect(DEFAUL_URL);
        
        $this->view->content_title = $this->view->title = "Delete Test";
        
        $obj = new Test();
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_Alpha');
            $filter = new Zend_Filter_Alpha();
            
            $id = (int)$this->_request->getPost('id');
            $del = $filter->filter($this->_request->getPost('del'));
            
            if ($del == 'Yes' && $id > 0) {
                $where = 'id = ' . $id;
                $obj->delete($where);
            }
        } else {
            $id = (int)$this->_request->getParam('id');
            if ($id > 0) {
                // only render if we have an id and can find the test.
                $this->view->item = $obj->fetchRow('id='.$id);
                
                if ($this->view->item->id > 0) {
                    $this->render();
                    return;
                }
            }
        }
        
        // redirect back to the project list unless we have rendered the view
        $this->_redirect('/testsetup');
    }
    
}